//
//  LeftCollectionViewCell.m
//  GentlemanAnime
//
//  Created by dlios on 15-7-6.
//  Copyright (c) 2015年 lanou3g.com 蓝欧科技. All rights reserved.
//
#define WIDTH self.contentView.frame.size.width
#define HEIGHT self.contentView.frame.size.height

#import "LeftCollectionViewCell.h"
#import "HomeTableViewCell.h"
#import "HomeTableViewCell2.h"
#import "UIImageView+WebCache.h"
#import "AppDelegate.h"
#import "DownloadViewController.h"
#import "DownloadSingletonTool.h"
@implementation LeftCollectionViewCell
- (void)dealloc {
    [_tableView release];
    [super dealloc];
}


- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
//        self.contentView.backgroundColor = [UIColor cyanColor];
        self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0 , WIDTH, HEIGHT) style:UITableViewStyleGrouped];
        [self.contentView addSubview:self.tableView];
        self.tableView.backgroundColor = [UIColor clearColor];
        UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 90 * HEIGHT / 480)];
        self.tableView.tableHeaderView = headerView;
        self.tableView.dataSource = self;
        self.tableView.delegate = self;
        
        [headerView release];
    }
    return self;
}

#pragma mark tableView的协议方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 2;
    } else if (section == 1) {
        if ([DownloadSingleton shareDownloadSingleton].collcetCount >= 15) {
            return 2;
        } else {
            return 1;
        }
    } else {
        return 1;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 1) {
        if ([DownloadSingleton shareDownloadSingleton].collcetCount >= 15) {
            if (indexPath.row == 0) {
                NSString *reuse1 = @"reuse1";
                HomeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse1];
                if (cell == nil) {
                    cell = [[HomeTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse1];
                }
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
                cell.titleLabel.text = @"移动网络观看";
                if ([DownloadSingleton shareDownloadSingleton].canSee) {
                    cell.statusSwitch.on = YES;
                } else {
                    cell.statusSwitch.on = NO;
                }
                return cell;
            } else {
                NSString *reuse2 = @"reuse2";
                HomeTableViewCell2 *cell = [tableView dequeueReusableCellWithIdentifier:reuse2];
                if (cell == nil) {
                    cell = [[HomeTableViewCell2 alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse2];
                }
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
                cell.titleLabel.text = @"麻吉特色";
                if ([DownloadSingleton shareDownloadSingleton].fuli) {
                    cell.statusSwitch.on = YES;
                } else {
                    cell.statusSwitch.on = NO;
                }
                return cell;
            }
        } else {
            NSString *reuse1 = @"reuse1";
            HomeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse1];
            if (cell == nil) {
                cell = [[HomeTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse1];
            }
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.titleLabel.text = @"移动网络观看";
            if ([DownloadSingleton shareDownloadSingleton].canSee) {
                cell.statusSwitch.on = YES;
            } else {
                cell.statusSwitch.on = NO;
            }
            return cell;
        }
    } else {
        static NSString *Identifier = @"Identifier";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Identifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Identifier];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = [UIColor clearColor];
        cell.textLabel.textColor = [UIColor whiteColor];
        cell.textLabel.font = [UIFont systemFontOfSize:15];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        if (indexPath.section == 0) {
            if (indexPath.row == 0) {
                cell.textLabel.text = @"我的收藏";
            } else {
                // 显示缓存大小
                CGFloat fileSize = [self fileSizeAtPath:[self getCachesPath]] / 1024.0;
                cell.textLabel.text = [NSString stringWithFormat:@"清除缓存(%.1fM)",fileSize];
            }
        } else {
            cell.textLabel.text = @"关于我们";
        }
        return cell;
    }
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0 && indexPath.row == 0) {
        //        [tableView deselectRowAtIndexPath:indexPath animated:YES];
        AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        [tempAppDelegate.LeftSlideVC closeLeftView];//关闭左侧抽屉
        
        DownloadViewController *downloadVC = [[DownloadViewController alloc] init];
        
        NSInteger index = tempAppDelegate.mainVC.tabBC.selectedIndex;
        UINavigationController *NaC = tempAppDelegate.mainVC.tabBC.viewControllers[index];
        [NaC pushViewController:downloadVC animated:NO];
        
        [downloadVC release];
    } else if (indexPath.section == 0 && indexPath.row == 1) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"确定要清除缓存吗?" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        alertView.tag = 1701;
        [alertView show];
        [alertView release];
    } else if (indexPath.section == 2) {
        AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        [tempAppDelegate.homeVC.collectionView setContentOffset:CGPointMake(WIDTH, 0)animated:YES];
    }
}

#pragma mark alertView点击事件方法
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag == 1701) {
        if (buttonIndex == 1) {
            // 直接删除缓存文件夹
            NSString *filePath = [self getCachesPath];
//            NSLog(@"%@",filePath);
            NSFileManager *manager = [NSFileManager defaultManager];
            [manager removeItemAtPath:filePath error:nil];
            // 删除缓存文件
            //            [[SDImageCache sharedImageCache] clearDisk];
            [self.tableView reloadData];
        }
    }
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 40;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section != 0) {
        return 1;
    } else {
        return 0;
    }
}

#pragma mark 获取缓存文件路径
-(NSString *)getCachesPath
{
    //获取Caches目录路径
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *cachesPath = [paths objectAtIndex:0];
    
    NSString *filePath = [cachesPath stringByAppendingPathComponent:@"com.hackemist.SDWebImageCache.default"];
    return filePath;
}

#pragma mark 计算缓存文件大小 M
-(long long)fileSizeAtPath:(NSString *) filePath
{
    //    NSLog(@"%@",filePath);
    NSFileManager *manager = [NSFileManager defaultManager];
    if ([manager fileExistsAtPath:filePath]) {
        return [[manager attributesOfItemAtPath:filePath error:nil] fileSize];
    }
    return 0;
}


@end
